package com.xiaoyi.springboot.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoyi.springboot.config.ResponseHelper;
import com.xiaoyi.springboot.config.ResponseModel;
import com.xiaoyi.springboot.entity.CommonUser;
import com.xiaoyi.springboot.entity.DriftingBottle;
import com.xiaoyi.springboot.entity.SysUser;
import com.xiaoyi.springboot.service.DriftingBottleService;
import com.xiaoyi.springboot.shiro.JwtUtil;
import com.xiaoyi.springboot.util.StringUtils;
import com.xiaoyi.springboot.vo.DriftingBottleQueryVO;
import com.xiaoyi.springboot.vo.SystemUserReqVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import scala.collection.script.Update$;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  漂流瓶
 * </p>
 *
 * @author xiaoyi
 * @since 2021-10-10
 */
@RestController
@Api(tags="漂流瓶")
@RequestMapping("/drifting-bottle")
public class DriftingBottleController {

    @Autowired
    private DriftingBottleService driftingBottleService;
    /*
     *  随机抓一个瓶子
     * @author xiaoyi
     * @date 2021/10/10 18:15
     * @param request
     * @return
     * @version v1.0
     */
    @ApiOperation("随机抓一个瓶子")
    @PostMapping("/takeDriftingBottle")
    public ResponseModel<DriftingBottle> takeDriftingBottle(HttpServletRequest request) {
        String userNameByToken = JwtUtil.getUserNameByToken(request);
        DriftingBottle driftingBottle= driftingBottleService.takeDriftingBottle(userNameByToken);
        return ResponseHelper.buildResponseModel(driftingBottle);
    }

    /*
     * 丢个瓶子
     * @author xiaoyi
     * @date 2021/10/10 20:01
     * @param request
     * @return com.xiaoyi.springboot.config.ResponseModel<com.xiaoyi.springboot.entity.DriftingBottle>
     * @version v1.0
     */
    @ApiOperation("丢个瓶子")
    @PostMapping("/throwDriftingBottle")
    public ResponseModel<Object> throwDriftingBottle(@RequestBody DriftingBottle driftingBottle ,HttpServletRequest request) {
        //todo 这里后面改成消息队列的形式
        String userName = JwtUtil.getUserNameByToken(request);
        String driftingBottleContent = driftingBottle.getDriftingBottleContent();
        Integer driftingBottleType = driftingBottle.getDriftingBottleType();
        if (StringUtils.isEmpty(driftingBottleContent)
                || Objects.isNull(driftingBottleType)){
            return ResponseHelper.buildResponseModel("ok");
        }
        driftingBottle.setUserAccount(userName);
        driftingBottleService.save(driftingBottle);
        return ResponseHelper.buildResponseModel("");
    }

    @ApiOperation("禁用")
    @PostMapping("/DisableDriftingBottle")
    public ResponseModel<Object> DisableDriftingBottle(Long driftingBottleId,HttpServletRequest request) {
        //todo 这里后面改成消息队列的形式
        String userName = JwtUtil.getUserNameByToken(request);
        DriftingBottle byId = driftingBottleService.getById(driftingBottleId);
        if (Objects.isNull(byId)){
            return ResponseHelper.validationFailure("禁用失败");
        }

        byId.setState(2);
        UpdateWrapper<DriftingBottle> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",driftingBottleId);
        driftingBottleService.update(byId,updateWrapper);
        return ResponseHelper.buildResponseModel("");
    }
    // 列表

    @PostMapping("/page")
    public ResponseModel<Page<DriftingBottle>> page(@RequestBody DriftingBottleQueryVO driftingBottleQueryVO,
                                             HttpServletRequest request) {
        String userName = JwtUtil.getUserNameByToken();
        QueryWrapper<DriftingBottle> queryWrapper = new QueryWrapper<>();
         if (Objects.nonNull(driftingBottleQueryVO.getState())){
             queryWrapper.eq("state",1);
         }
        if (Objects.nonNull(driftingBottleQueryVO.getUserAccount())){
            queryWrapper.eq("userAccount",driftingBottleQueryVO.getUserAccount());
        }
        if (Objects.nonNull(driftingBottleQueryVO.getDriftingBottleContent())){
            queryWrapper.like("driftingBottleContent",driftingBottleQueryVO.getDriftingBottleContent());
        }
         queryWrapper.orderBy(true, false, "createTime");
        Page<DriftingBottle> sysUserPage = new Page<>(driftingBottleQueryVO.getPageNum(), driftingBottleQueryVO.getPageSize());
        Page<DriftingBottle> page = driftingBottleService.page(sysUserPage,queryWrapper);
        return ResponseHelper.buildResponseModel(page);
    }




}
